﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using CarFrameWork.Common;
namespace CarFrameWork.Model
{
	/// <summary>
	/// 类UserInfo。
	/// </summary>
	[Serializable]
	public partial class UserInfo
	{
		public UserInfo()
		{}
		#region Model
		private int _userid;
		private string _loginname;
		private string _password;
		private string _username;
		private int? _roleid;
		private int? _groupid;
		private string _description;
		/// <summary>
		/// 
		/// </summary>
		public int UserID
		{
			set{ _userid=value;}
			get{return _userid;}
		}
		/// <summary>
		/// 
		/// </summary>
		public string LoginName
		{
			set{ _loginname=value;}
			get{return _loginname;}
		}
		/// <summary>
		/// 
		/// </summary>
		public string PassWord
		{
			set{ _password=value;}
			get{return _password;}
		}
		/// <summary>
		/// 
		/// </summary>
		public string UserName
		{
			set{ _username=value;}
			get{return _username;}
		}
		/// <summary>
		/// 
		/// </summary>
		public int? RoleID
		{
			set{ _roleid=value;}
			get{return _roleid;}
		}
		/// <summary>
		/// 
		/// </summary>
		public int? GroupID
		{
			set{ _groupid=value;}
			get{return _groupid;}
		}
		/// <summary>
		/// 
		/// </summary>
		public string Description
		{
			set{ _description=value;}
			get{return _description;}
		}
		#endregion Model


		#region  Method

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public UserInfo(int UserID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select UserID,LoginName,PassWord,UserName,RoleID,GroupID,Description ");
			strSql.Append(" FROM [UserInfo] ");
			strSql.Append(" where UserID=@UserID ");
			SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.Int,4)};
			parameters[0].Value = UserID;

			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["UserID"]!=null && ds.Tables[0].Rows[0]["UserID"].ToString()!="")
				{
					this.UserID=int.Parse(ds.Tables[0].Rows[0]["UserID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["LoginName"]!=null && ds.Tables[0].Rows[0]["LoginName"].ToString()!="")
				{
					this.LoginName=ds.Tables[0].Rows[0]["LoginName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["PassWord"]!=null && ds.Tables[0].Rows[0]["PassWord"].ToString()!="")
				{
					this.PassWord=ds.Tables[0].Rows[0]["PassWord"].ToString();
				}
				if(ds.Tables[0].Rows[0]["UserName"]!=null && ds.Tables[0].Rows[0]["UserName"].ToString()!="")
				{
					this.UserName=ds.Tables[0].Rows[0]["UserName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["RoleID"]!=null && ds.Tables[0].Rows[0]["RoleID"].ToString()!="")
				{
					this.RoleID=int.Parse(ds.Tables[0].Rows[0]["RoleID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["GroupID"]!=null && ds.Tables[0].Rows[0]["GroupID"].ToString()!="")
				{
					this.GroupID=int.Parse(ds.Tables[0].Rows[0]["GroupID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Description"]!=null && ds.Tables[0].Rows[0]["Description"].ToString()!="")
				{
					this.Description=ds.Tables[0].Rows[0]["Description"].ToString();
				}
			}
		}

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{

		return DbHelperSQL.GetMaxID("UserID", "UserInfo"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int UserID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from [UserInfo]");
			strSql.Append(" where UserID=@UserID ");

			SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.Int,4)};
			parameters[0].Value = UserID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}

        /// <summary>
		/// 是否存在该记录
		/// </summary>
        public bool Exists(string LoginName, string PassWord)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from [UserInfo]");
            //strSql.Append(where);
            strSql.Append(" where LoginName=@LoginName  and PassWord=@PassWord");

			SqlParameter[] parameters = {
					new SqlParameter("@LoginName", SqlDbType.NVarChar,50),
					new SqlParameter("@PassWord", SqlDbType.NVarChar,50)};
            parameters[0].Value = LoginName;
            parameters[1].Value = PassWord;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
		}

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string LoginName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [UserInfo]");
            //strSql.Append(where);
            strSql.Append(" where LoginName=@LoginName");

            SqlParameter[] parameters = {
					new SqlParameter("@LoginName", SqlDbType.NVarChar,50)};
            parameters[0].Value = LoginName;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add()
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into [UserInfo] (");
			strSql.Append("LoginName,PassWord,UserName,RoleID,GroupID,Description)");
			strSql.Append(" values (");
			strSql.Append("@LoginName,@PassWord,@UserName,@RoleID,@GroupID,@Description)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@LoginName", SqlDbType.NVarChar,50),
					new SqlParameter("@PassWord", SqlDbType.NVarChar,50),
					new SqlParameter("@UserName", SqlDbType.NVarChar,50),
					new SqlParameter("@RoleID", SqlDbType.Int,4),
					new SqlParameter("@GroupID", SqlDbType.Int,4),
					new SqlParameter("@Description", SqlDbType.NVarChar)};
			parameters[0].Value = LoginName;
			parameters[1].Value = PassWord;
			parameters[2].Value = UserName;
			parameters[3].Value = RoleID;
			parameters[4].Value = GroupID;
			parameters[5].Value = Description;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update()
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update [UserInfo] set ");
			strSql.Append("LoginName=@LoginName,");
			strSql.Append("PassWord=@PassWord,");
			strSql.Append("UserName=@UserName,");
			strSql.Append("RoleID=@RoleID,");
			strSql.Append("GroupID=@GroupID,");
			strSql.Append("Description=@Description");
			strSql.Append(" where UserID=@UserID ");
			SqlParameter[] parameters = {
					new SqlParameter("@LoginName", SqlDbType.NVarChar,50),
					new SqlParameter("@PassWord", SqlDbType.NVarChar,50),
					new SqlParameter("@UserName", SqlDbType.NVarChar,50),
					new SqlParameter("@RoleID", SqlDbType.Int,4),
					new SqlParameter("@GroupID", SqlDbType.Int,4),
					new SqlParameter("@Description", SqlDbType.NVarChar),
					new SqlParameter("@UserID", SqlDbType.Int,4)};
			parameters[0].Value = LoginName;
			parameters[1].Value = PassWord;
			parameters[2].Value = UserName;
			parameters[3].Value = RoleID;
			parameters[4].Value = GroupID;
			parameters[5].Value = Description;
			parameters[6].Value = UserID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int UserID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from [UserInfo] ");
			strSql.Append(" where UserID=@UserID ");
			SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.Int,4)};
			parameters[0].Value = UserID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public void GetModel(int UserID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select UserID,LoginName,PassWord,UserName,RoleID,GroupID,Description ");
			strSql.Append(" FROM [UserInfo] ");
			strSql.Append(" where UserID=@UserID ");
			SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.Int,4)};
			parameters[0].Value = UserID;

			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["UserID"]!=null && ds.Tables[0].Rows[0]["UserID"].ToString()!="")
				{
					this.UserID=int.Parse(ds.Tables[0].Rows[0]["UserID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["LoginName"]!=null && ds.Tables[0].Rows[0]["LoginName"].ToString()!="")
				{
					this.LoginName=ds.Tables[0].Rows[0]["LoginName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["PassWord"]!=null && ds.Tables[0].Rows[0]["PassWord"].ToString()!="")
				{
					this.PassWord=ds.Tables[0].Rows[0]["PassWord"].ToString();
				}
				if(ds.Tables[0].Rows[0]["UserName"]!=null && ds.Tables[0].Rows[0]["UserName"].ToString()!="")
				{
					this.UserName=ds.Tables[0].Rows[0]["UserName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["RoleID"]!=null && ds.Tables[0].Rows[0]["RoleID"].ToString()!="")
				{
					this.RoleID=int.Parse(ds.Tables[0].Rows[0]["RoleID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["GroupID"]!=null && ds.Tables[0].Rows[0]["GroupID"].ToString()!="")
				{
					this.GroupID=int.Parse(ds.Tables[0].Rows[0]["GroupID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Description"]!=null && ds.Tables[0].Rows[0]["Description"].ToString()!="")
				{
					this.Description=ds.Tables[0].Rows[0]["Description"].ToString();
				}
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM [UserInfo] ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

        /// <summary>
        /// 获取权限编号
        /// </summary>
        /// <param name="loginName"></param>
        /// <returns></returns>
        public int GetRoleId(string loginName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  RoleID ");
            strSql.Append(string.Format(" FROM [UserInfo] Where LoginName='{0}'", loginName));
            return Convert.ToInt32(DbHelperSQL.Query(strSql.ToString()).Tables[0].Rows[0][0]);
        }


        /// <summary>
        /// 获取小组编号
        /// </summary>
        /// <param name="loginName"></param>
        /// <returns></returns>
        public int GetGroupId(string loginName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  GroupID ");
            strSql.Append(string.Format(" FROM [UserInfo] Where LoginName='{0}'", loginName));
            return Convert.ToInt32(DbHelperSQL.Query(strSql.ToString()).Tables[0].Rows[0][0]);
        }

		#endregion  Method

        #region Login
        public string Login(string loginName, string password)
        {
            string message = string.Empty;
            //loginName = "admin1";
            //password = "123456";
            try
            {
                //m_UserInfoDA = UserInfoDA.Get(string.Format("where [LoginName]='{0}' and [PassWord]='{1}'", loginName,password));
                bool m_UserInfoDA = this.Exists(loginName,password);

                if (m_UserInfoDA == false)
                {
                    message = "登陆帐号不存在！";
                }

                //if (string.IsNullOrEmpty(message) && !PassWord.Equals(password))
                //{
                //    message = "登陆密码不正确！";
                //}

                //if (string.IsNullOrEmpty(message) && Role == 0)
                //{
                //    message = "用户没有权限,使用本系统！";
                //}

                //if (string.IsNullOrEmpty(message))
                //{
                //    CurrentUser.Instance.User = this;
                //    CurrentUser.Instance.Role = new Role();
                //    CurrentUser.Instance.Role.Retrieve(Role);
                //    CurrentUser.Instance.Modules = CurrentUser.Instance.Role.Modules;
                //    CurrentUser.Instance.Edition = new Edition();
                //    CurrentUser.Instance.Edition.Retrieve(Edition);
                //}
            }
            catch
            {
                message = "请与管理员联系！非正常错误！";
            }

            return message;
        }
        #endregion
	}
}